<?php
/**
 * Clase Base DP para BaseMrpRecetaDetalleDP
 *
 * @package    Recetas
 * @subpackage BaseMrpRecetaDetalleDP
 * @author     hans
 * @version    SVN:: BaseMrpRecetaDetalleDP.class.php,v 1.1 2012-03-07 02:01:11 hans.parra Exp $
 */

require_once($_SERVER['DOCUMENT_ROOT'].'/gourmet-express/trunk/src/class/common/arrayClass.class.php');
require_once($_SERVER['DOCUMENT_ROOT'].'/gourmet-express/trunk/src/class/mrp/recetas/BN/MrpRecetaDetalleBN.class.php');
abstract class BaseMrpRecetaDetalleDP extends MrpRecetaDetalleBN
{
  /* N�mero de registros afectados */
  private $nRowsAffected;

  public function __construct($bSetDefault = true)
  {
    if( $bSetDefault instanceof MrpRecetaDetalleBN )
    {
      $o_MrpRecetaDetalleBN = $bSetDefault;
      if($o_MrpRecetaDetalleBN->getRcdInsumoId()) : $this->setRcdInsumoId($o_MrpRecetaDetalleBN->getRcdInsumoId()); endif;
      if($o_MrpRecetaDetalleBN->getRctRecetaId()) : $this->setRctRecetaId($o_MrpRecetaDetalleBN->getRctRecetaId()); endif;
      if($o_MrpRecetaDetalleBN->getRcdInsumoCant()) : $this->setRcdInsumoCant($o_MrpRecetaDetalleBN->getRcdInsumoCant()); endif;
      if($o_MrpRecetaDetalleBN->getRcdUoM()) : $this->setRcdUoM($o_MrpRecetaDetalleBN->getRcdUoM()); endif;
      if($o_MrpRecetaDetalleBN->getRcdPorcMerma()) : $this->setRcdPorcMerma($o_MrpRecetaDetalleBN->getRcdPorcMerma()); endif;
      if($o_MrpRecetaDetalleBN->getRcdAlmacenId()) : $this->setRcdAlmacenId($o_MrpRecetaDetalleBN->getRcdAlmacenId()); endif;
      if($o_MrpRecetaDetalleBN->getSysInsUsr()) : $this->setSysInsUsr($o_MrpRecetaDetalleBN->getSysInsUsr()); endif;
      if($o_MrpRecetaDetalleBN->getSysInsAt()) : $this->setSysInsAt($o_MrpRecetaDetalleBN->getSysInsAt()); endif;
      if($o_MrpRecetaDetalleBN->getSysLstUpdUsr()) : $this->setSysLstUpdUsr($o_MrpRecetaDetalleBN->getSysLstUpdUsr()); endif;
      if($o_MrpRecetaDetalleBN->getSysLstUpdAt()) : $this->setSysLstUpdAt($o_MrpRecetaDetalleBN->getSysLstUpdAt()); endif;
    }
  }

  /* Obtiene el n�mero de registros afectados */
  public function getRowsAffected()
  {
    return $this->nRowsAffected;
  }

  public function save()
  {
    try
    {
      $o_conexion = new BaseDatos();
      $con=$o_conexion->conectar();

      if($this->getRcdInsumoId() && $this->getRctRecetaId()){
        $a_Fields = array();
 
        if($this->getRcdInsumoId()) : $a_Fields['rcd_insumoId'] = ($this->getRcdInsumoId()); endif;
        if($this->getRctRecetaId()) : $a_Fields['rct_recetaId'] = ($this->getRctRecetaId()); endif;
        if($this->getRcdInsumoCant()) : $a_Fields['rcd_insumoCant'] = ($this->getRcdInsumoCant()); endif;
        if($this->getRcdUoM()) : $a_Fields['rcd_UoM'] = ($this->getRcdUoM()); endif;
        if($this->getRcdPorcMerma()) : $a_Fields['rcd_porcMerma'] = ($this->getRcdPorcMerma()); endif;
        if($this->getRcdAlmacenId()) : $a_Fields['rcd_almacenId'] = ($this->getRcdAlmacenId()); endif;
        if($this->getSysInsUsr()) : $a_Fields['SysInsUsr'] = ($this->getSysInsUsr()); endif;
        if($this->getSysInsAt()) : $a_Fields['SysInsAt'] = ($this->getSysInsAt()); endif;
        if($this->getSysLstUpdUsr()) : $a_Fields['SysLstUpdUsr'] = ($this->getSysLstUpdUsr()); endif;
        if($this->getSysLstUpdAt()) : $a_Fields['SysLstUpdAt'] = ($this->getSysLstUpdAt()); endif;

        $sStringField = arrayClass::implode_keys(',', $a_Fields);
        $sStringValue = "'".implode("','", $a_Fields)."'";

        $sql = "INSERT INTO MRP_RECETADETALLE (".$sStringField.") VALUES (".$sStringValue.");";
        $result = mysql_db_query('main_db', $sql);
        if($result)
        {
          $this->nRowsAffected = (int) mysql_affected_rows($con);
        }
        else
        {
          throw new Exception( mysql_error($con) );
        }
      }
      else
      {
        throw new Exception( 'El id no esta registrado.');
      }
    }
    catch ( Exception $o_Error )
    {
      throw new Exception($o_Error->getMessage().' en '.$o_Error->getFile().' linea '.$o_Error->getLine());
    }
    return true;
  }

  public function update()
  {
    try
    {
      if($this->getRcdInsumoId() && $this->getRctRecetaId())
      {
        $a_Fields = array();

        if($this->getRcdInsumoId()) : $a_Fields['rcd_insumoId'] = ($this->getRcdInsumoId()); endif;
        if($this->getRctRecetaId()) : $a_Fields['rct_recetaId'] = ($this->getRctRecetaId()); endif;
        if($this->getRcdInsumoCant()) : $a_Fields['rcd_insumoCant'] = ($this->getRcdInsumoCant()); endif;
        if($this->getRcdUoM()) : $a_Fields['rcd_UoM'] = ($this->getRcdUoM()); endif;
        if($this->getRcdPorcMerma()) : $a_Fields['rcd_porcMerma'] = ($this->getRcdPorcMerma()); endif;
        if($this->getRcdAlmacenId()) : $a_Fields['rcd_almacenId'] = ($this->getRcdAlmacenId()); endif;
        if($this->getSysInsUsr()) : $a_Fields['SysInsUsr'] = ($this->getSysInsUsr()); endif;
        if($this->getSysInsAt()) : $a_Fields['SysInsAt'] = ($this->getSysInsAt()); endif;
        if($this->getSysLstUpdUsr()) : $a_Fields['SysLstUpdUsr'] = ($this->getSysLstUpdUsr()); endif;
        if($this->getSysLstUpdAt()) : $a_Fields['SysLstUpdAt'] = ($this->getSysLstUpdAt()); endif;

        $sStringField = arrayClass::implode_key_value(',', $a_Fields);

				$o_conexion = new BaseDatos();
				$con=$o_conexion->conectar();
        $sql = "UPDATE MRP_RECETADETALLE SET ".$sStringField." WHERE 1 AND ='".$this->getRcdInsumoId()."' AND RCT_RECETAID='".$this->getRctRecetaId()."';";
        $result = mysql_db_query('main_db', $sql);
        if($result)
        {
          $this->nRowsAffected = (int) mysql_affected_rows($con);
        }
        else
        {
          throw new Exception( mysql_error($con) );
        }
      }
      else
      {
        throw new Exception( 'El id no esta registrado.');
      }
    }
    catch ( Exception $o_Error )
    {
      throw new Exception($o_Error->getMessage().' en '.$o_Error->getFile().' linea '.$o_Error->getLine());
    }
    return true;
  }

  public function delete_By_RecetaId()
  {
    try
    {
      if( $this->getRctRecetaId())
      {
        $a_Fields = array();

        $o_conexion = new BaseDatos();
		$con=$o_conexion->conectar();
        $sql = "DELETE FROM MRP_RECETADETALLE WHERE 1  AND rct_recetaId='".$this->getRctRecetaId()."';"; 
        $result = mysql_db_query('main_db', $sql);
        if($result)
        {
          $this->nRowsAffected = (int) mysql_affected_rows($con);
        }
        else
        {
          throw new Exception( mysql_error($con) );
        }
      }
      else
      {
        throw new Exception( 'El id no esta registrado.');
      }
    }
    catch ( Exception $o_Error )
    {
      throw new Exception($o_Error->getMessage().' en '.$o_Error->getFile().' linea '.$o_Error->getLine());
    }
    return true;
  }
  
  public function delete()
  {
    try
    {
      if($this->getRcdInsumoId() && $this->getRctRecetaId())
      {
        $a_Fields = array();

        $o_conexion = new BaseDatos();
		$con=$o_conexion->conectar();
        $sql = "DELETE FROM MRP_RECETADETALLE WHERE 1 AND rcd_insumoId ='".$this->getRcdInsumoId()."' AND rct_recetaId='".$this->getRctRecetaId()."';"; 
        $result = mysql_db_query('main_db', $sql);
        if($result)
        {
          $this->nRowsAffected = (int) mysql_affected_rows($con);
        }
        else
        {
          throw new Exception( mysql_error($con) );
        }
      }
      else
      {
        throw new Exception( 'El id no esta registrado.');
      }
    }
    catch ( Exception $o_Error )
    {
      throw new Exception($o_Error->getMessage().' en '.$o_Error->getFile().' linea '.$o_Error->getLine());
    }
    return true;
  }

  /* B�squeda del registro por la llave primaria */
  public static function selectByPK($vRcdInsumoId, $vRctRecetaId)
  {
    try
    {
			$o_conexion = new BaseDatos();
			$con=$o_conexion->conectar();
      $sql = "SELECT rcd_insumoId, rct_recetaId, rcd_insumoCant, rcd_UoM, rcd_porcMerma, rcd_almacenId, SysInsUsr, SysInsAt, SysLstUpdUsr, SysLstUpdAt  FROM MRP_RECETADETALLE WHERE 1 AND rcd_insumoId='".$vRcdInsumoId."' AND rct_recetaId='".$vRctRecetaId."'; ";
      $result = mysql_db_query('main_db', $sql);
      $o_Propel = $result ? mysql_fetch_object($result) : null;
      
      return $o_Propel?self::insertObjectBN($o_Propel):null;

    }catch ( Exception $o_Error ){
      throw new Exception($o_Error->getMessage().' en '.$o_Error->getFile().' linea '.$o_Error->getLine());
    }
  }

  /* Devuelve un objeto con m�todos de acceso */
  private function insertObjectBN($o_propel){
    $o_MrpRecetaDetalleDP = new MrpRecetaDetalleDP();
    $o_MrpRecetaDetalleDP->setRcdInsumoId($o_propel->rcd_insumoId);
    $o_MrpRecetaDetalleDP->setRctRecetaId($o_propel->rct_recetaId);
    $o_MrpRecetaDetalleDP->setRcdInsumoCant($o_propel->rcd_insumoCant);
    $o_MrpRecetaDetalleDP->setRcdUoM($o_propel->rcd_UoM);
    $o_MrpRecetaDetalleDP->setRcdPorcMerma($o_propel->rcd_porcMerma);
    $o_MrpRecetaDetalleDP->setRcdAlmacenId($o_propel->rcd_almacenId);
    $o_MrpRecetaDetalleDP->setSysInsUsr($o_propel->SysInsUsr);
    $o_MrpRecetaDetalleDP->setSysInsAt($o_propel->SysInsAt);
    $o_MrpRecetaDetalleDP->setSysLstUpdUsr($o_propel->SysLstUpdUsr);
    $o_MrpRecetaDetalleDP->setSysLstUpdAt($o_propel->SysLstUpdAt);
    return $o_MrpRecetaDetalleDP;
  }
}